package com.ljn.data.web.controller;

import com.ljn.data.entity.PageParam;
import com.ljn.data.entity.PageView;
import com.ljn.data.entity.Pagination;
import com.ljn.data.utils.JsonUtils;
import com.ljn.data.utils.ServletUtils;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.dom4j.tree.AbstractEntity;
import org.springframework.util.StringUtils;
import org.springframework.web.servlet.ModelAndView;

public class BaseControllerSupport extends AbstractController {
    protected static final String ACTION_STATUS_FAIL = "fail";
    protected static final String ACTION_STATUS_SUCCESS = "success";
    private Map<String, Object> jsonResult = new HashMap(8);

    public BaseControllerSupport() {
    }

    public final ModelAndView createModelAndView(String viewName, PageView pageView) {
        ModelAndView mav = new ModelAndView();
        mav.setViewName(viewName);
        mav.addObject("pv", pageView);
        return mav;
    }

    public final String createModelAndJsonView(List<?> list) {
        return this.createModelAndJsonView(true, (String)null, (Object)list);
    }

    public final String createModelAndJsonView(Map<String, Object> map) {
        return this.createModelAndJsonView(true, (String)null, (Object)map);
    }

    public final String createModelAndJsonView(Pagination<?> pagination) {
        return this.createModelAndJsonView(true, (String)null, (Object)pagination);
    }

    public final String createModelAndJsonView(PageView pageView) {
        return this.createModelAndJsonView(true, (String)null, (PageView)pageView);
    }

    public final String createModelAndJsonView(AbstractEntity entity) {
        return this.createModelAndJsonView(true, (String)null, (Object)entity);
    }

    public final String createModelAndJsonView(Boolean actionStatus, String actionMessage) {
        return this.createModelAndJsonView(actionStatus, actionMessage, (PageView)null);
    }

    public final String createModelAndJsonView(Boolean actionStatus, String actionMessage, PageView pageView) {
        this.jsonResult.put("actionStatus", this.getActionStatusString(actionStatus));
        if (!StringUtils.isEmpty(actionMessage)) {
            this.jsonResult.put("actionMessage", actionMessage);
        }

        if (pageView != null && !pageView.getModelMap().isEmpty()) {
            this.jsonResult.put("data", pageView.getModelMap());
        }

        return JsonUtils.toJsonString(this.jsonResult);
    }

    private String createModelAndJsonView(Boolean actionStatus, String actionMessage, Object object) {
        this.jsonResult.put("actionStatus", this.getActionStatusString(actionStatus));
        if (!StringUtils.isEmpty(actionMessage)) {
            this.jsonResult.put("actionMessage", actionMessage);
        }

        if (object != null) {
            this.jsonResult.put("data", object);
        }

        return JsonUtils.toJsonString(this.jsonResult);
    }

    /** @deprecated */
    @Deprecated
    public final String setJsonResult(Object data) {
        return this.setJsonResult(true, (String)null, data);
    }

    /** @deprecated */
    @Deprecated
    public final String setJsonResult(Boolean actionStatus, String actionMessage) {
        return this.setJsonResult(actionStatus, actionMessage, (Object)null);
    }

    /** @deprecated */
    @Deprecated
    public final String setJsonResult(Boolean actionStatus, String actionMessage, Object data) {
        this.jsonResult.put("actionStatus", this.getActionStatusString(actionStatus));
        if (!StringUtils.isEmpty(actionMessage)) {
            this.jsonResult.put("actionMessage", actionMessage);
        }

        if (data != null) {
            this.jsonResult.put("data", data);
        }

        return JsonUtils.toJsonString(this.jsonResult);
    }

    public final PageParam getPageParam() {
        int pageNum = ServletUtils.getIntParameter(super.getRequest(), "p", 1);
        int numPerPage = ServletUtils.getIntParameter(super.getRequest(), "ps", 15);
        if (pageNum == 0) {
            pageNum = ServletUtils.getIntParameter(super.getRequest(), "pageNum", 1);
        }

        if (numPerPage == 0) {
            numPerPage = ServletUtils.getIntParameter(super.getRequest(), "numPerPage", 15);
        }

        return new PageParam(pageNum, numPerPage > 200 ? 200 : numPerPage);
    }

    private String getActionStatusString(Boolean actionStatus) {
        return actionStatus ? "success" : "fail";
    }
}

